<?php
Class DossierDAO
{
	public $connexion;

	public function __construct()
	{
		try {
			$this->connexion = new PDO("mysql:host=".SERVEUR_MYSQL.";dbname=".BASE.";charset=utf8", UTILISATEUR, M2P);
		}
		catch(PDOException $e)
		{
			echo "Erreur : ".$e->getMessage();
			die();
		}
	}


	public function rechercher_id_dossier($parPhenotypePersonne,$typeCategorie,$categorie,$parCentre)
	{
		$query = "SELECT d.iddossier "
		."FROM dossier as d ";

		if ($parPhenotypePersonne != null){
			$query .= ", phenotype_dossier as pd ";
		}
		if ($typeCategorie != null || $categorie){
			$query .= ", categorie_biomed cb";
		}
		$query .=" WHERE 1 ";
		if ($parPhenotypePersonne != null){
			$query .= ", phenotype_dossier as pd ";
		}
		if ($typeCategorie != null || $categorie){
			$query .= ", categorie_biomed cb";
		}
		if ($parPhenotypePersonne != null){
			$tabIdPhenotype = split("",$parPhenotypePersonne);
			$query .= " pd.iddossier = d.iddossier AND pd.idphenotype in (";
			foreach ($tablIdPhenotype as $idPhenotype){
				$query .= $idPhenotype.",";
			}
			$query = substr($query, 0, -1);
			$query .= ") ";
		}
		if ($typeCategorie != null && count($typeCategorie) > 0){
			$query .= " AND idcategoriebiomed in (";
			foreach ($typeCategorie as $tc){

			}
		}
		if ($parCentre != null && $parCentre != 0){
			$query .= " AND idcentre = :idcentre";
		}

		$requette_preparee = $this->connexion->prepare($query);
		if ($parCentre != null){
			$requette_preparee->bindValue(":idcentre" , $parCentre, PDO::PARAM_INT);
		}
		if ($parCategorie != null){
			$requette_preparee->bindValue(":idcategoriebiomed" , $parCategorie, PDO::PARAM_INT);
		}

		$requette_preparee->execute();
		$dossiers = $requette_preparee->fetchAll(PDO::FETCH_CLASS,'Dossier');
		return  $dossiers;

	} // rechercher_dossier

	public function recupere_dossier_par_id_pour_tableau_recherche($iddossier)
	{
		$query = "SELECT d.iddossier, d.numero, d.sexe, cat.libelle as categorie , prel.libelle as typeprelevement, mp.libelle as marquepuce, tp.libelle as typepuce , count(cnv.idcnv) as nbcnv, d.formulechromosomique, c.nom  "
		."FROM dossier as d ";
		$query .= ", type_puce as tp ";
		$query .= ", marque_puce as mp ";
		$query .= ", type_prelevement as prel ";
		$query .= ", centre as c ";
		$query .= ", categorie_biomed as cat ";
		$query .= ", cnv as cnv ";
		$query .=" WHERE 1 ";
		$query .=" AND tp.idtypepuce = d.idtypepuce ";
		$query .=" AND mp.idmarquepuce = tp.idmarquepuce ";
		$query .=" AND d.idcentre = c.idcentre ";
		$query .=" AND cnv.iddossier = d.iddossier ";
		$query .=" AND cat.idcategorie = d.idcategoriebiomed ";
		$query .=" AND prel.idtypeprelevement = d.idtypeprelevement ";
		if ($iddossier != null && $iddossier != 0){
			$query .= " AND d.iddossier = :iddossier";
		}
		$requette_preparee = $this->connexion->prepare($query);
		if ($iddossier != null){
			$requette_preparee->bindValue(":iddossier" , $iddossier, PDO::PARAM_INT);
		}
		$requette_preparee->execute();
		$dossiers = $requette_preparee->fetchAll();
		return  $dossiers;

	} // rechercher_dossier

	public function ajouter_dossier($dossier)
	{


	} // ajouter_dossier

} // DossierDAO



?>
